//
//  HJXTableViewCell.m
//  Catus
//
//  Created by 晓梦影 on 16/4/22.
//  Copyright © 2016年 黄金星. All rights reserved.
//

#import "HJXTableViewCell.h"
#import "HJXHeadLinesItem.h"
#import "UIImageView+WebCache.h"
#import "UIImage+clip.h"



@interface HJXTableViewCell ()

/** 头像*/
@property (weak, nonatomic) IBOutlet UIImageView *logoImageV;
/** 昵称*/
@property (weak, nonatomic)IBOutlet UILabel *nameL;
/** 发布时间*/
@property (weak, nonatomic) IBOutlet UILabel *dateL;
/** 标题*/
@property (weak, nonatomic) IBOutlet UILabel *titleL;
/** 正文*/
@property (weak, nonatomic) IBOutlet UILabel *textL;
/** 评论数*/
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;
/** 赞数*/
@property (weak, nonatomic) IBOutlet UIButton *likeBtn;
/** 属性*/
@property (weak, nonatomic) IBOutlet UILabel *attrL;
/** 配图*/
@property (weak, nonatomic) IBOutlet UIImageView *iconImageV;
/** 赞相对于正文的约束高度*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageVHHeight;

@end
@implementation HJXTableViewCell

- (void)awakeFromNib {
 // 先调用awakeFromNib然后再调用heightForRowAtIndexPath
    // 手动设置文字的最大宽度(目的:为了让label能够计算自己最真实的高度)
    self.textL.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
    // 设置赞 | 评论按钮
    [self.likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.likeBtn setFont:[UIFont systemFontOfSize:12]];
    [self.likeBtn setImage:[UIImage imageNamed:@"ButtonLike"] forState:UIControlStateNormal];
    [self.likeBtn setImage:[UIImage imageNamed:@"ButtonLikeSelect"] forState:UIControlStateSelected];
    [self.commentBtn setImage:[UIImage imageNamed:@"reply"] forState:UIControlStateNormal];
    [self.commentBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.commentBtn setFont:[UIFont systemFontOfSize:12]];
    [self.likeBtn addTarget:self action:@selector(likeBtnSelected:) forControlEvents:UIControlEventTouchDown];
    [self.commentBtn addTarget:self action:@selector(commentBtnClick:) forControlEvents:UIControlEventTouchDown];
    
}

#pragma mark - 点击按钮
- (void)likeBtnSelected:(UIButton *)likeBtn
{
    if (self.likeBtn.selected == YES) {
        self.likeBtn.selected = NO;
    }else {
        self.likeBtn.selected = YES;
    }
}

- (void)commentBtnClick:(UIButton *)commentBtn
{
    
}
/*
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 添加所有的子view
        [self.contentView sd_addSubviews:@[self.logoImageV,self.nameL,self.dateL,self.titleL,self.textL,self.iconImageV,self.attrL,self.likeBtn,self.commentBtn]];
    }
    
    return self;
}
*/
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


/** 返回cell的高度*/
- (CGFloat)cellHeight
{
    // 强制布局(目的:让label根据约束计算自己最真实的尺寸)
    [self layoutIfNeeded];
    CGFloat cellHeight = 0;

        cellHeight = CGRectGetMaxY(self.attrL.frame) + 10;
    
    return cellHeight;
}

- (void)setItem:(HJXHeadLinesItem *)item
{
    
//    _item = item;
//    self.logoImageV.image = [UIImage imageNamed:item.HeadLogo];
//    self.titleL.text = item.HeadTitle;
//    self.nameL.text = item.HeadName;
//    self.textL.text = item.HeadText;
//    [self.likeBtn setTitle:item.HeadLike forState:UIControlStateNormal];
//    [self.commentBtn setTitle:item.HeadComment forState:UIControlStateNormal];
//    self.attrL.text = item.HeadAttr;
//    // 设置赞数相对于正文的约束
//    self.iconImageVHHeight.constant = item.headIcon == nil ? 10 : 10 + self.iconImageV.frame.size.height;
//    self.iconImageV.hidden = item.headIcon == nil;
//    self.iconImageV.image = [UIImage imageNamed:item.headIcon];
//
    _item = item;
    
    
    // 对头像进行裁剪
    UIImage *newImage = [UIImage clipWith:[UIImage imageNamed:@"head"]];
    self.logoImageV.image = newImage;
    self.titleL.text = item.title;
    self.nameL.text = item.userName;
    self.textL.text = item.bodyBrief;
    [self.likeBtn setTitle:@"11" forState:UIControlStateNormal];
    [self.commentBtn setTitle:item.replyNum forState:UIControlStateNormal];
    self.attrL.text = @"大杂烩";
    // 设置赞数相对于正文的约束
//    self.iconImageVHHeight.constant = item.images == nil ? 10 : 10 + self.iconImageV.frame.size.height;
//    self.iconImageV.hidden = item.images == nil;
    
    // 当数据为空或者null，再或者是 http://3g.mop.com/cms/client/index.html 格式，那就不显示图片
    if ([item.images isEqualToString:@""] || [ item.images isEqualToString:@"null"] || [item.images containsString:@"html"]) {
        self.iconImageVHHeight.constant = 10;
        self.iconImageV.hidden = YES;
    }else
    {
        self.iconImageVHHeight.constant = 10 + self.iconImageV.frame.size.height;
        self.iconImageV.hidden = NO;
    }
    
    // 下载图片，设置配图
    // SDWebImage
    [self.iconImageV sd_setImageWithURL:[NSURL URLWithString:item.images] placeholderImage:[UIImage imageNamed:@"LoadingImage"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        // 监听进度
    }];

    
  }



@end
